from django.db.models import Count

from apps.app_friend.models import Friend
from apps.app_post.models import Post, Tag
from apps.app_talk.models import Talk


def front_context():
    """
    前台模板所需数据
    :return:
    """
    like_posts = Post.objects.select_related("category", 'author').order_by('-like')
    recom_posts = Post.objects.select_related("category", 'author').filter(is_recom=True)
    tags = Tag.objects.annotate(posts_num=Count('posts')).all()
    talks = Talk.objects.select_related('mood').order_by('-pub_time')
    friends = Friend.objects.order_by('-pub_time')
    return {
        "like_posts": like_posts[0:6],
        "talks": talks[0:6],
        "tags": tags,
        "recom_posts": recom_posts[0:6],
        "friends": friends[0:6],
    }
